package com.liu.queue.agreement;

import com.liu.common.Constants;
import com.liu.generator.Generator;
import com.liu.generator.impl.Generators;
import com.liu.queue.message.MessageTypeEnum;
import com.liu.serialization.SerializationTypeEnum;
import com.liu.util.HashFixedLength;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class Agreement {
    // 魔数
    private int magicNumber;
    // 版本号
    private byte version;
    // 消息类型
    private MessageTypeEnum messageType;
    // 序列化类型
    private SerializationTypeEnum serializationType;
    // 消息主题
    private byte[] topic;
    // 消息ID
    private Long messageID;
    // 消息体
    private Object body;

    private Agreement(int magicNumber, byte version, MessageTypeEnum messageType, SerializationTypeEnum serializationType, byte[] topic, Long messageID, Object body) {
        this.magicNumber = magicNumber;
        this.version = version;
        this.messageType = messageType;
        this.serializationType = serializationType;
        this.topic = topic;
        this.messageID = messageID;
        this.body = body;
    }

    public Agreement(int magicNumber, byte version, MessageTypeEnum messageType, SerializationTypeEnum serializationType, String  topic, Generator messageID, Object body) {
        this.magicNumber = magicNumber;
        this.version = version;
        this.messageType = messageType;
        this.serializationType = serializationType;
        this.topic = HashFixedLength.encode(topic);
        this.messageID = messageID.generateMessageID();
        this.body = body;
    }

    public Agreement(int magicNumber, byte version, MessageTypeEnum messageType, SerializationTypeEnum serializationType, byte[] topic, long messageID) {
        this.magicNumber = magicNumber;
        this.version = version;
        this.messageType = messageType;
        this.serializationType = serializationType;
        this.topic = topic;
        this.messageID = messageID;
    }


    public static Agreement Default(String topic,Object body) {
        return new Agreement(Constants.MAGIC_NUMBER,
                Constants.VERSION,
                MessageTypeEnum.PUSH_ORDINARY_REQUEST,
                SerializationTypeEnum.JSON,
                topic,
                Generators.IdWorker,
                body);
    }

}